<?php

class Application_Model_Image
{
    /**
     * @param int $productID
     * @return ImagePOJO[]
     */
    public static function getImages($productID = null)
    {
        $dbAdapter = JDbAdapter::factory();

        $dbSelect = $dbAdapter->select()->from('IMAGES');
        if (isset($productID)) {
            $dbSelect->where('ProductID = ?', $productID);
        }

        $assoc = $dbAdapter->fetchAll($dbSelect);
        $imagePOJOs = array();
        foreach ($assoc as $row) {
            $imagePOJO = new ImagePOJO($row);
            array_push($imagePOJOs, $imagePOJO);
        }

        return $imagePOJOs;
    }
}

/**
 * @property-read string $URL
 */
class ImagePOJO extends POJO
{
    /** @var int */
    public $ID;

    /** @var string */
    public $Name;

    /** @var int */
    public $ProductID;

    /**
     * @return void
     */
    protected function convert()
    {
        $this->ID = (int)$this->ID;
        $this->Name = (string)$this->Name;
        $this->ProductID = (int)$this->ProductID;
    }

    /**
     * @param string $name
     * @return mixed
     * @throws Exception
     */
    public function __get($name)
    {
        switch ($name) {
            case 'URL':
                return JImageDB::getImageURL($this->Name);
            default:
                throw new Exception("cannot get $name property");
        }
    }
}

